package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "C端地址簿接口")
@RestController
@Slf4j
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;
    @ApiOperation("新增地址")
    @PostMapping
    public Result<String> addAddress(@RequestBody AddressBook addressBook){
        log.info("新增地址:{}",addressBook);
        addressBookService.addAddress(addressBook);
        return Result.success();
    }
    @ApiOperation("查询登录用户所有地址")
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        List<AddressBook> addressBookList = addressBookService.queryList();
        return Result.success(addressBookList);
    }

    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getdefault(){
        AddressBook addressBook = addressBookService.getDefault();
        return Result.success(addressBook);
    }

    @ApiOperation("修改地址")
    @PutMapping
    public Result<String> updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }

    @ApiOperation("根据id删除地址")
    @DeleteMapping
    public Result<String> deleteAddress(@RequestParam Long id){
        addressBookService.deleteAddress(id);
        return Result.success();
    }

    @ApiOperation("根据id查询地址信息")
    @GetMapping("/{id}")
    public Result<AddressBook> getAddressById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getAddressById(id);
        if(addressBook == null){
            return Result.error("默认地址不存在");
        }
        return Result.success(addressBook);
    }

    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result<String> setDefault(@RequestBody AddressBook addressBook){
        addressBookService.setDefault(addressBook);
        return Result.success();
    }
}
